package com.wxmsg;

import com.common.Constant;
import com.soecode.wxtools.api.IService;
import com.soecode.wxtools.api.WxMessageHandler;
import com.soecode.wxtools.bean.WxXmlMessage;
import com.soecode.wxtools.bean.WxXmlOutMessage;
import com.soecode.wxtools.exception.WxErrorException;
import com.stock.StockMsg;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Arrays;
import java.util.Map;

/**
 * Created by lzb on 2018/5/11.
 */
public class DefaultHandle implements WxMessageHandler {

    private RedisTemplate redisTemplate = (RedisTemplate) Constant.ctx.getBean("redisTemplate");

    @Override
    public WxXmlOutMessage handle(WxXmlMessage wxMessage, Map<String, Object> context, IService iService) throws WxErrorException {

        WxXmlOutMessage xmlOutMsg = WxXmlOutMessage.TEXT().content(

        StockMsg.getPrice(Arrays.asList(
                (String[]) redisTemplate.opsForSet().members(wxMessage.getFromUserName()).toArray(new  String[]{})
                ),
                Arrays.asList((String[]) redisTemplate.opsForSet().members(wxMessage.getFromUserName()+"_BUY").toArray(new  String[]{}))
        )

        ).toUser(wxMessage.getFromUserName()).fromUser(wxMessage.getToUserName()).build();

        return xmlOutMsg;
    }
}
